cmake_minimum_required(VERSION 3.20)
#程序名称
Set(PROJECT_NAME base_sdl2)
#主版本
set(PROJECT_MAJOR_VERSION_STR 1)
#次版本号
set(PROJECT_MINOR_VERSION_STR 0)
#修订号
set(PROJECT_REVISION_VERSION_STR 0)
#构建版本（以时间戳天数作为构建版本）
string(TIMESTAMP PROJECT_BUILD_TIME_SECOND "%s" UTC)
math(EXPR  PROJECT_BUILD_VERSION_STR "${PROJECT_BUILD_TIME_SECOND}/60/60/24"  OUTPUT_FORMAT DECIMAL)
#程序版本
set(PROJECT_VERSION_STR "${PROJECT_MAJOR_VERSION_STR}.${PROJECT_MINOR_VERSION_STR}.${PROJECT_REVISION_VERSION_STR}.${PROJECT_BUILD_VERSION_STR}")
#工程根目录
set(PROJECT_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})


Project(${PROJECT_NAME}	VERSION "${PROJECT_VERSION_STR}")
Project(${PROJECT_NAME} C CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 14)

#判断是否为Emscripten
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten")
	set(WASM_BUILD ON)
	message(STATUS "This is a wasm build!")
else()
	set(WASM_BUILD OFF)
endif()

if(${WASM_BUILD})
	set(CMAKE_EXECUTABLE_SUFFIX ".html")
	add_compile_definitions(WASM_BUILD=1)
	add_compile_options(-sUSE_SDL_TTF=2)
	add_compile_definitions(HAVE_SDL2=1)
	add_compile_options(-sUSE_SDL=2)
endif()

file(GLOB MAIN_CXX_C_FILES *.cpp  *.c *.h)
add_executable(${PROJECT_NAME}  ${MAIN_CXX_C_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

if(${WASM_BUILD})
	#设定一些选项
	target_link_libraries(${PROJECT_NAME}  -sWASM_BIGINT=1 -sUSE_SDL=2 -sUSE_SDL_TTF=2)
endif()

include(FindPkgConfig)
#添加线程库
FIND_PACKAGE(Threads REQUIRED)
TARGET_LINK_LIBRARIES(${PROJECT_NAME}  ${CMAKE_THREAD_LIBS_INIT})

if(NOT ${WASM_BUILD})
	include(FindPkgConfig)
	#sdl2
	find_package(sdl2 REQUIRED)
	if(${SDL2_SDL2main_FOUND})
		TARGET_LINK_LIBRARIES(${PROJECT_NAME} SDL2::SDL2main)
	endif()
	if(${SDL2_SDL2test_FOUND})
		TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2TEST_LIBRARY})
	endif()
	TARGET_LINK_LIBRARIES(${PROJECT_NAME}  SDL2::SDL2)
	target_include_directories(${PROJECT_NAME}  PUBLIC ${SDL2_INCLUDE_DIRS})
	#sdl2_image
	find_package(sdl2_image REQUIRED)
	if(${SDL2_image_FOUND})
		TARGET_LINK_LIBRARIES(${PROJECT_NAME} SDL2_image::SDL2_image)
	endif()
	#sdl2_mixer
	find_package(sdl2_mixer REQUIRED)
	if(${SDL2_mixer_FOUND})
		TARGET_LINK_LIBRARIES(${PROJECT_NAME} SDL2_mixer::SDL2_mixer)
	endif()
	#sdl2_net
	find_package(sdl2_net REQUIRED)
	if(${SDL2_net_FOUND})
		TARGET_LINK_LIBRARIES(${PROJECT_NAME} SDL2_net::SDL2_net)
	endif()
	#sdl2_ttf
        find_package(sdl2_ttf REQUIRED)
	if(${SDL2_ttf_FOUND})
		TARGET_LINK_LIBRARIES(${PROJECT_NAME} SDL2_ttf::SDL2_ttf)
	endif()
endif()

#添加common_fsloader库文件
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../common/fsloader common_fsloader EXCLUDE_FROM_ALL)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} common_fsloader)

#添加common_font库文件
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../common/font common_font)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} common_font)

#添加3rdparty/HCppBox
set(HCPPBOX_USE_PURE_CPLUSPLUS ON)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../3rdparty/HCppBox HCppBox EXCLUDE_FROM_ALL)
#启用HCppBox、HRC
hcppbox_enable(${PROJECT_NAME})
hrc_enable(${PROJECT_NAME})


include(GNUInstallDirs)
if(${WASM_BUILD})
INSTALL(FILES ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.html  ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.data ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.js ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.wasm
       DESTINATION /var/www/html/wasm/${PROJECT_NAME}/
)
endif()


